<?php

namespace plugins\message_push\controller;

use cmf\controller\PluginAdminBaseController;

class AdminIndexController extends PluginAdminBaseController
{

    /**
     * 消息配置管理模块 菜单注解
     * @adminMenu(
     * 		'name'		=>	'消息配置管理',
     * 		'parent'	=>	'admin/Plugin/default',
     * 		'display'	=>	true,
     * 		'hasView'	=>	true,
     * 		'order'		=>	9999,
     * 		'icon'		=>	'',
     * 		'remark'	=>	'消息配置管理',
     * 		'param'		=>	''
     * )
     */
	public function index()
	{
        $param = $this->request->param();
        $this->assign('company_id', $param['id']);
        return $this->fetch();
	}

    /**
     * 获取所有配置信息
     */
    public function getList(){
        $param = $this->request->param();
        if(empty($param['company_id'])){
            return api_json_echo(false,'未获取到公司信息');
        }
        //邮件
        $email_config = db('email_config')->where('company_id',$param['company_id'])->find();
        //短信
        $key = '';
        $secret = '';
        $sms_config = db('sms_config')->where('company_id',$param['company_id'])->find();
        if(!empty($sms_config)){
            $key = $sms_config['key'];
            $secret = $sms_config['secret'];
        }
        $temp_arr = db('sms_config')->where('company_id',$param['company_id'])->select()->toArray();
        if(empty($temp_arr)){
            $temp_arr_item['temp_tag'] = '';
            $temp_arr_item['temp_name'] = '';
            $temp_arr_item['temp_autograph'] = '';
            $temp_arr_item['temp_code'] = '';
            $temp_arr_item['temp_remark'] = '';
            $temp_arr[] = $temp_arr_item;
        }
        //小程序
        $applet_config = db('applet_config')->where('company_id',$param['company_id'])->find();
        //公众号
        $official_config = db('official_config')->where('company_id',$param['company_id'])->find();
        //个推
        $getui_config = db('getui_config')->where('company_id',$param['company_id'])->find();
        $res = [
            'email_form'=>$email_config,
            'sms_form'=>[
                'key'=>$key,
                'secret'=>$secret,
                'temp_arr'=>$temp_arr
            ],
            'applet_form'=>$applet_config,
            'official_form'=>$official_config,
            'getui_form'=>$getui_config
        ];
        return api_json_echo(true,'查询成功',$res);
    }

    /**
     * 修改邮件配置
     */
    public function editEmail(){
        $param = $this->request->param();
        if(empty($param['company_id'])){
            return api_json_echo(false,'未获取到公司信息');
        }
        $email_config = db('email_config')->where('company_id',$param['company_id'])->find();
        $param['edit_time'] = time();
        if(empty($email_config)){
            //添加
            $res = db('email_config')->strict(false)->insert($param);
        }else{
            //修改
            $res = db('email_config')->where('company_id',$param['company_id'])->strict(false)->update($param);
        }
        if($res){
            return api_json_echo(true,'提交成功');
        }
        return api_json_echo(false,'提交失败');
    }

    /**
     * 修改短信配置
     */
    public function editSms(){
        $param = $this->request->param();
        if(empty($param['company_id'])){
            return api_json_echo(false,'未获取到公司信息');
        }
        if(!empty($param['sms_del_ids'])){
            db('sms_config')->where('id','in',$param['sms_del_ids'])->delete();
        }
        foreach ($param['temp_arr'] as $item) {
            $item['key'] = $param['key'];
            $item['secret'] = $param['secret'];
            $item['edit_time'] = time();
            $item['company_id'] = $param['company_id'];
            if(empty($item['id'])){
                db('sms_config')->strict(false)->insert($item);
            }else{
                db('sms_config')->where('id',$item['id'])->update($item);
            }
        }
        return api_json_echo(true,'提交成功');
    }

    /**
     * 修改小程序配置
     */
    public function editApplet(){
        $param = $this->request->param();
        if(empty($param['company_id'])){
            return api_json_echo(false,'未获取到公司信息');
        }
        $applet_config = db('applet_config')->where('company_id',$param['company_id'])->find();
        $param['edit_time'] = time();
        if(empty($applet_config)){
            //添加
            $res = db('applet_config')->strict(false)->insert($param);
        }else{
            //修改
            $res = db('applet_config')->where('company_id',$param['company_id'])->strict(false)->update($param);
        }
        if($res){
            return api_json_echo(true,'提交成功');
        }
        return api_json_echo(false,'提交失败');
    }

    /**
     * 修改公众号配置
     */
    public function editOfficial(){
        $param = $this->request->param();
        if(empty($param['company_id'])){
            return api_json_echo(false,'未获取到公司信息');
        }
        $official_config = db('official_config')->where('company_id',$param['company_id'])->find();
        $param['edit_time'] = time();
        if(empty($official_config)){
            //添加
            $res = db('official_config')->strict(false)->insert($param);
        }else{
            //修改
            $res = db('official_config')->where('company_id',$param['company_id'])->strict(false)->update($param);
        }
        if($res){
            return api_json_echo(true,'提交成功');
        }
        return api_json_echo(false,'提交失败');
    }

    /**
     * 修改个推配置
     */
    public function editGetui(){
        $param = $this->request->param();
        if(empty($param['company_id'])){
            return api_json_echo(false,'未获取到公司信息');
        }
        $getui_config = db('getui_config')->where('company_id',$param['company_id'])->find();
        $param['edit_time'] = time();
        if(empty($getui_config)){
            //添加
            $res = db('getui_config')->strict(false)->insert($param);
        }else{
            //修改
            $res = db('getui_config')->where('company_id',$param['company_id'])->strict(false)->update($param);
        }
        if($res){
            return api_json_echo(true,'提交成功');
        }
        return api_json_echo(false,'提交失败');
    }
}